{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 中间代码，计算信息熵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原来信息熵为:\n",
      "0.6517565846443176\n",
      "新划分后的信息熵为:\n",
      "0.6730116605758667\n",
      "0.6869615316390991\n",
      "6.678682606953146e-11\n",
      "0.6730116605758667\n"
     ]
    }
   ],
   "source": [
    "#coding:utf-8\n",
    "import torch\n",
    "import pdb\n",
    "# data create\n",
    "\n",
    "# info gain\n",
    "\n",
    "# class_list=[[9],[4]]\n",
    "# class_lsit=[[3,2],[4,0],[2,3]]\n",
    "# class_lsit=[[3,2],[4,0],[2,3]]\n",
    "# class_lsit=[[3,2],[4,0],[2,3]]\n",
    "\n",
    "\n",
    "\n",
    "def get_emtropy(class_list):\n",
    "#    pdb.set_trace()\n",
    "   E = 0\n",
    "   sumv = float(sum(class_list))\n",
    "   if sumv == 0:\n",
    "        # 为了防止计算不出结果\n",
    "       sumv =0.000000000001\n",
    "   for cl in class_list:\n",
    "       if cl==0:\n",
    "           cl=0.00000000001\n",
    "       p = torch.tensor(float(cl/sumv))\n",
    "       E += -1.0 * p*torch.log(p)\n",
    "   return E.item()\n",
    "   \n",
    "# 二分类决策树\n",
    "if __name__==\"__main__\":\n",
    "    print(\"原来信息熵为:\")\n",
    "    print(get_emtropy([9,5]))\n",
    "    print(\"新划分后的信息熵为:\")\n",
    "    # 右侧五个\n",
    "    print(get_emtropy([3,2]))\n",
    "    # 左侧九个\n",
    "    print(get_emtropy([4,5]))    \n",
    "    print(get_emtropy([4,0]))\n",
    "    print(get_emtropy([3,2]))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
